AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

include('shared.lua')

/*---------------------------------------------------------
   Name: Initialize
---------------------------------------------------------*/
function ENT:Initialize()
	self.Entity:SetModel("models/weapons/w_missile_closed.mdl")
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_VPHYSICS)
	self.Entity:SetKeyValue("physdamagescale", "9999")
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableGravity(false)
	end
	self.DidHit = false
	self.GoofyTiem = CurTime()
	self.Upgraded = false
	self.RocketSound = CreateSound(self.Entity,"Missile.Ignite")
    self.RocketSound:Play()
	timer.Create("fuel",15,1,function() self.DidHit = true end)
end

function ENT:Think()
	if (ValidEntity(self.Entity:GetOwner())==false) then
		self.Entity:Remove()
	end
	self.Entity:GetPhysicsObject():SetVelocity(self.Entity:GetForward()*800)
	
	self.Owner:SetMoveType(MOVETYPE_NONE)
    if self.Entity:GetPhysicsObject():IsAsleep() then
        self.Entity:GetPhysicsObject():Wake()
    end
    if (self.Owner:KeyDown(IN_FORWARD)) and (!self.Owner:KeyDown(IN_BACK)) and (self:GetAngles().p >= -88) then
        self:SetAngles(self:GetAngles()+Angle(-2,0,0))
    elseif (self.Owner:KeyDown(IN_BACK)) and (!self.Owner:KeyDown(IN_FORWARD)) and (self:GetAngles().p <= 88) then
        self:SetAngles(self:GetAngles()+Angle(2,0,0))
    elseif (self.Owner:KeyDown(IN_FORWARD)) and (self.Owner:KeyDown(IN_BACK)) then
        self:SetAngles(self:GetAngles()+Angle(0,0,0))
    else
        self:SetAngles(self:GetAngles()+Angle(0,0,0))    
    end
    
    if (self.Owner:KeyDown(IN_MOVELEFT)) and (!self.Owner:KeyDown(IN_MOVERIGHT)) then
        self:SetAngles(self:GetAngles()+Angle(0,2,0))
    elseif (self.Owner:KeyDown(IN_MOVERIGHT)) and (!self.Owner:KeyDown(IN_MOVELEFT)) then
        self:SetAngles(self:GetAngles()+Angle(0,-2,0))
    elseif (self.Owner:KeyDown(IN_MOVELEFT)) and (self.Owner:KeyDown(IN_MOVERIGHT)) then
        self:SetAngles(self:GetAngles()+Angle(0,0,0))
    else
        self:SetAngles(self:GetAngles()+Angle(0,0,0))    
    end
        
    if (self.Owner:KeyPressed(IN_ATTACK) || self.Owner:Alive() == false)then
		self.DidHit = true
    end
    
    self:NextThink(CurTime()+0.01)
    return true
	
end

function ENT:PhysicsCollide(data, physobj)
	data.HitNormal = data.HitNormal*-150
	local start = data.HitPos+data.HitNormal
	local endpos = data.HitPos+data.HitNormal*-1
	util.Decal("Scorch",start,endpos)

  	self.DidHit = true
	self.Entity:GetPhysicsObject():SetVelocity(Vector(0,0,0))
end 

function ENT:PhysicsUpdate()
	local trace = {}
		trace.start = self.Entity:GetPos()
		trace.endpos = self.Entity:GetPos()
		trace.filter = {self.Entity, self.Entity:GetOwner()}
	trace = util.TraceLine(trace)
	if (trace.Hit && (ValidEntity(trace.Entity) && trace.Entity:GetClass()!="shot_guidedmissle")) || (trace.Hit && !ValidEntity(trace.Entity)) then self.DidHit=true end
	if (self.DidHit == true) then

		util.BlastDamage(self.Entity, self.Entity:GetOwner(), self.Entity:GetPos(), 300, 50)
		util.BlastDamage(self.Entity, self.Entity:GetOwner(), self.Entity:GetPos(), 80, 100)
		
		local effectdata = EffectData()
		effectdata:SetStart(self.Entity:GetPos())
		effectdata:SetOrigin(self.Entity:GetPos())
		effectdata:SetScale(1.5)
		util.Effect("HelicopterMegaBomb", effectdata)
	
		local effectdata2 = EffectData()
		effectdata2:SetStart(self.Entity:GetPos())
		effectdata2:SetOrigin(self.Entity:GetPos())
		effectdata2:SetScale(1.5) 
		util.Effect("Explosion", effectdata2)
		
		local shake = ents.Create("env_shake")
		shake:SetOwner(self.Owner)
		shake:SetPos(self.Entity:GetPos())
		shake:SetKeyValue("amplitude", "3000")	// Power of the shake
		shake:SetKeyValue("radius", "1000")		// Radius of the shake
		shake:SetKeyValue("duration", "1.0")	// Time of shake
		shake:SetKeyValue("frequency", "255")	// How har should the screenshake be
		shake:SetKeyValue("spawnflags", "4")	// Spawnflags(In Air)
		shake:Spawn()
		shake:Activate()
		shake:Fire("StartShake", "", 0)
	
		self.Entity:Fire("kill", "", 0)
		self.Entity:EmitSound(Sound("weapons/explode3.wav"))
		self.Entity:Remove()
	end
end

function ENT:Upgrade() 
	self.Upgrade = true
end

function ENT:OnRemove()	
    self.Owner:SetMoveType(MOVETYPE_WALK)
    self.Owner:SetViewEntity(self.Owner)
	self.RocketSound:Stop()
end